package com.bfb.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bfb.Details;
import com.bfb.R;
import com.bfb.helpers.Dish;
import com.customComponents.ImageText;

public class DishAdapter extends BaseAdapter {
	private Context context;
	private int itemBackground;
	ArrayList<Dish> dishes = new ArrayList<Dish>();
	public OnItemClickListener listener;
	Gallery gallery;

	public DishAdapter(Context c, Gallery galleryInput,
			ArrayList<Dish> dishesInput) {
		this.gallery = galleryInput;
		context = c;
		dishes = dishesInput;
		// ---setting the style---
		TypedArray a = context.obtainStyledAttributes(R.styleable.Gallery1);
		itemBackground = a.getResourceId(
				R.styleable.Gallery1_android_galleryItemBackground, 0);
		a.recycle();
		listener = new OnItemClickListener() {
			// Once a thumbnail from the gallery is clicked the user is taken to
			// a view with more details of the dish selected
			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				// http://stackoverflow.com/questions/6729263/setselection-on-gallery-is-resulting-wrong-item-selected
				if (gallery.getSelectedItemPosition() == position) {
					Intent i = new Intent(context, Details.class);
					i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

					try {
						i.putExtra("Key", dishes.get(position).getDishKey()); // This
						// refers to

						Log.e("dish key", dishes.get(position).getDishKey());
						// the key
						context.startActivity(i); // of
						// the
						// dish in the database
					} catch (Exception e) {
						// TODO: handle exception
						Log.e("Exception", e.getMessage());
					}

				} else
					gallery.setSelection(position);
			}
		};
	}

	public View makeView() {

		ImageView iView = new ImageView(context);
		iView.setScaleType(ImageView.ScaleType.FIT_CENTER);

		iView.setLayoutParams(new ImageSwitcher.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		iView.setBackgroundColor(0xFF000000);

		return iView;
	}

	// ---returns the number of images---
	@Override
	public int getCount() {
		return dishes.size();
	}

	// ---returns the ID of an item---
	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	// ---returns an ImageView view---
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageText it = new ImageText(context, null);

		ImageView imageView = it.getImageView();

		// Sets the thumbnail in the gallery to the corresponding array of
		// bitmap array got from appengine. In case no image was available
		// for the dish the default image is used
		if (dishes.get(position).getDishImage() == null)
			imageView.setImageResource(R.drawable.default_dish);
		else
			imageView.setImageBitmap(dishes.get(position).getDishImage());

		// imageView.setScaleType(ImageView.ScaleType.FIT_XY);

		imageView.setLayoutParams(new LinearLayout.LayoutParams(150, 120));
		imageView.setBackgroundResource(itemBackground);

		TextView tv = it.getTextView();
		// The String that holds the details of the corresponding dish
		String dishDetails = dishes.get(position).getDishName();

		tv.setText(dishDetails);
		tv.setGravity(Gravity.CENTER);
		tv.setWidth(150);
		tv.setHeight(120);
		tv.setTextColor(0xffffffff);

		tv.setTextSize(8);
		tv.setPadding(0, 0, 0, 40);

		return it;

	}
}